var GRABABLE_MASK_BIT = 1 << 31;
var NOT_GRABABLE_MASK = ~GRABABLE_MASK_BIT;
var DENSITY = 1/1000;

classes.layers.Game = cc.Layer.extend({
    isMouseDown:false,
    player:null,
    enemy:null,
    KeyEnum: {
        UP: 0,
        DOWN: 1, 
        LEFT: 2,
        RIGHT: 3
    },
    keysPressed: null,
    init:function(playerName) {
        this._super();

        this.keysPressed = new Array(false, false, false, false);
        var size = cc.Director.getInstance().getWinSize();
        var cache = cc.SpriteFrameCache.getInstance();
        cache.addSpriteFrames("res/" + playerName + "_spritesheet.plist", "res/" + playerName + ".png");
        cache.addSpriteFrames("res/stepan.plist", "res/stepan.png");

        var lazyLayer = cc.Layer.create();
        this.addChild(lazyLayer);



        this.space = new cp.Space();
        var self = this;
        /*this.space.setDefaultCollisionHandler(function(arb, space) {
            var arr = arb.getShapes();
            
            if(arr[0] === self.player.shape) {
                if(arr[1] === self.enemy.shape) {
                    if(self.enemy.dead) return false;
                    if(self.player.getBody().getVel().y < 0) {
                        if(self.player.getPosition().y > self.enemy.getPosition().y+40) {
                            if(self.enemy.hitCount == 5) { self.enemy.hit = true; return false; }
                                self.player.hit = true;
                                self.enemy.hit = true;
                            return true;
                        }
                    }
                    return false;
                }
            } else if(arr[0] === self.enemy.shape) {
                if(arr[1] === self.player.shape) {
                    if(self.enemy.dead) return false;
                    if(self.player.getBody().getVel().y < 0) {
                        if(self.player.getPosition().y > self.enemy.getPosition().y+40) {
                            if(self.enemy.hitCount == 5) { self.enemy.hit = true; return false; }
                            self.player.hit = true;
                            self.enemy.hit = true;
                        }
                    }
                    return false;
                }
            }
            return true;
        });*/
        this.space.gravity = cp.v(0,-1500);

        this.addChild(new classes.sprites.Box("res/box.png", this.space, cc.p(0,0), cc.p(2000, 30)));
        this.addChild(new classes.sprites.Box("res/box.png", this.space, cc.p(2000,0), cc.p(2000, 30)));

        //this.addChild(new classes.sprites.Box("res/platform.png", this.space, cc.p(300,120), cc.p(200, 30)), -6);


        var body = this.space.addBody(new cp.Body(30*70*DENSITY, Infinity));
        var box = new cp.BoxShape(body, 30, 70);
        box.setFriction(0.8);
        box.setElasticity(0);
        this.space.addShape(box);
        

        // add "HelloWorld" splash screen"
        this.player = new classes.sprites.Player("Stand_left.png", body);
        this.player.setPosition(cc.p(size.width / 2, size.height / 2));
        this.player.shape = box;
        this.scheduleUpdate();


        this.follow = cc.Follow.create(this.player, cc.rect(0,0,Infinity, 480));
        this.runAction(this.follow);

        lazyLayer.addChild(this.player, 0);

        body = this.space.addBody(new cp.Body(30*70*DENSITY, Infinity));
        box = new cp.BoxShape(body, 30, 70);
        this.space.addShape(box);
        box.setFriction(0.8);
        box.setElasticity(0);

        //this.enemy = new classes.sprites.Enemy("sWalk_left0.png", body);
        //this.enemy.setPosition(cc.p(50, size.height / 2));
        //this.enemy.shape = box;

        //this.addChild(this.enemy);

        this.initBackground();
        this.initTiledMap();

        this.setTouchEnabled(true);
        this.setKeyboardEnabled(true);
        return true;
    },
    initBackground: function() {
        this.background = new classes.sprites.BackgroundManager();
        this.background.init();
        this.addChild(this.background.sprites[0], -8);
        this.addChild(this.background.sprites[1], -8);
    },
    initTiledMap: function() {
        this.tiledMap = new classes.maps.TiledHallway();
        this.addChild(this.tiledMap, -7);

        for( var i = 0; i < this.tiledMap.obstacles.length; i++) {
            var body = cp.StaticBody();
            body.setPos(this.tiledMap.obstacles[i].origin);
            var shape = new cp.BoxShape(body, this.tiledMap.obstacles[i].width, this.tiledMap.obstacles[i].height);
            shape.setElasticity(0);
            shape.setFriction(1);
            shape.setLayers(NOT_GRABABLE_MASK);
            this.space.addShape(shape);
        }
        //var startPoint = objectGroup.objectNamed("StartPosition");
    },
    onTouchesBegan:function (touches, event) {
        app.getAudio().playEffect('res/coin.mp3',false);
        if(touches[0].getLocation().x > 600) {
            this.player.move = true;
            this.player.direction = 5;
            this.player.spriteFrameNamePrefix = "Walk_right";
        } else if(touches[0].getLocation().x < 200) {
            this.player.move = true;
            this.player.direction = -5;
            this.player.spriteFrameNamePrefix = "Walk_left";
        } else {
            this.player.jump();
        }
        
    },
    onTouchesEnded:function (touches, event) {
        this.player.move = false;
        if(this.player.spriteFrameNamePrefix === "Walk_left")
            this.player.spriteFrameNamePrefix = "Stand_left";
        else
            this.player.spriteFrameNamePrefix = "Stand_right";
        this.player.generateNewSprite(this.player.spriteFrameNamePrefix + '.png');
        this.player.walkAnim = 0;
    },
    onKeyDown: function(keyCode) {
        if(keyCode == cc.KEY.left || keyCode == cc.KEY.right) {
            
            if (keyCode == cc.KEY.left) {
                this.keysPressed[this.KeyEnum.LEFT] = true;
                this.player.spriteFrameNamePrefix = "Walk_left";
                this.player.direction = -5;
            }
            else {
                this.keysPressed[this.KeyEnum.RIGHT] = true;
                this.player.spriteFrameNamePrefix = "Walk_right";
                this.player.direction = 5;
            }


            this.player.walking = true;
            this.player.move = true;
            var indexAsString;

            indexAsString=this.player.spriteFrameIndex.toString();

            

        }
        if(keyCode == 38) {
            this.player.jump();
            this.keysPressed[this.KeyEnum.UP] = true;
        }

    },
    onKeyUp: function(keyCode) {
        if(keyCode == cc.KEY.left) {
            this.keysPressed[this.KeyEnum.LEFT] = false;
        } else if(keyCode == cc.KEY.right) {
            this.keysPressed[this.KeyEnum.RIGHT] = false;
        } else if(keyCode == cc.KEY.up) {
            this.keysPressed[this.KeyEnum.UP] = false;
        }
        if(this.keysPressed[this.KeyEnum.LEFT] == false &&  this.keysPressed[this.KeyEnum.RIGHT] == false) {
            this.player.move = false;
            if(this.player.spriteFrameNamePrefix === "Walk_left")
                this.player.spriteFrameNamePrefix = "Stand_left";
            else
                this.player.spriteFrameNamePrefix = "Stand_right";
            this.player.generateNewSprite(this.player.spriteFrameNamePrefix + '.png');
            this.player.walkAnim = 0;
        }
    }, update: function(dt) {
        //this.enemy.myUpdate(this.player);
        this.player.myUpdate();
        this.space.step(dt);
        this.background.update(this.player.getPosition().x);
        /*if(this.enemy.dead && this.enemy.deadTimer >= 60) {
            this.space.removeShape(this.enemy.shape);
            this.space.removeBody(this.enemy.getBody());
            this.removeChild(this.enemy);
            var body = this.space.addBody(new cp.Body(30*70*DENSITY, Infinity));
            var box = new cp.BoxShape(body, 30, 70);
            this.space.addShape(box);
            box.setFriction(0.8);
            box.setElasticity(0);

            this.enemy = new classes.sprites.Enemy("sWalk_left0.png", body);
            this.enemy.setPosition(cc.p(50, 300));
            this.enemy.shape = box;

            this.addChild(this.enemy);
        }*/
    }
});